﻿// ==PREPROCESSOR==
// @name "Scroll Text"
// @version "2.3"
// @author "kgena_ua modifed AHAPXICT"
// ==/PREPROCESSOR==

function RGB(r,g,b) { return (0xff000000|(r<<16)|(g<<8)|(b)); }
function RGBA(r, g, b, a) { return ((a << 24) | (r << 16) | (g << 8) | (b)); }

MF_SEPARATOR = 0x00000800;
MF_STRING = 0x00000000;

var ww,wh;
var txt_row = 0;
var s, step, step1 = window.Width, step2 = window.Width;
var text1, text2, t_width, t_width1, t_width2, dir, sw = 1;
var t1 = "", t2 = "", t3 = "", ti = "";
var font = gdi.Font("Arial",24,0);  // FONT
var show1 = window.GetProperty("|1| Title", true);
var show2 = window.GetProperty("|2| Artist", true);
var show3 = window.GetProperty("|3| Album", true);
var showi = window.GetProperty("|4| Info", true);
var mode = window.GetProperty("|5| Scrolling Mode", false);
var font_rgb, backcolor_rgb = window.GetProperty("|6| Color", 1);
var speed = window.GetProperty("|7| Speed Scroll", 40); // speed = 1 (fastest) to 50 (slowest)
var reverse = window.GetProperty("|8| Reverse Left to Right", false);

function on_paint(gr) {
    ww = window.Width;
    wh = window.Height;
    if (window.GetProperty("|6| Color") == 1) font_rgb = RGB(0,0,0), backcolor_rgb = RGB(255,255,255);      //Default "Black & White"
    if (window.GetProperty("|6| Color") == 2) font_rgb = RGB(128,192,255), backcolor_rgb = RGB(19,30,38);   //Blue
    if (window.GetProperty("|6| Color") == 3) font_rgb = RGB(0,255,0), backcolor_rgb = RGB(18,38,38);       //Green
    if (window.GetProperty("|6| Color") == 4) font_rgb = RGB(255,0,0), backcolor_rgb = RGB(26,10,10);       //Red
    if (window.GetProperty("|6| Color") == 5) font_rgb = RGB(255,255,255), backcolor_rgb = RGB(0,0,0);      //White "White & Black"
    if (window.GetProperty("|6| Color") == 6) font_rgb = RGB(255,255,0), backcolor_rgb = RGB(26,38,38);     //Yellow
    if (window.GetProperty("|6| Color") == 7) font_rgb = RGB(85,136,171), backcolor_rgb = RGB(22,36,45);    //Grey    
    
    if (show1) {t1 = fb.TitleFormat("$upper([%title%])").Eval();
    } else {t1 = "";}

    if (show2) {t2 = fb.TitleFormat("$upper([%artist%])").Eval();
    } else {t2 = "";}

    if (show3) {t3 = fb.TitleFormat("$upper([%album%])[ - %date%]").Eval();
    } else {t3 = "";}

    if (showi) {ti = fb.TitleFormat("[$info(encoding)]$if(%__hdcd%, | HDCD,)[ | $info(codec)][ $info(codec_profile)][ | $info(bitrate) kbps][ | $info(samplerate) Hz][ | $info(bitspersample) bps][ | $channels()]$if($stricmp($ext(%filename_ext%),cue), | '['cue']',[ | %filesize_natural%])$if($strcmp($info(cue_embedded),yes), | '['embedded cue']',)").Eval();
    } else {ti = "";}

    text1 = t1 + (show1 > 0 && t2 != "" ? " - " : "") + t2 + ((show1 > 0 || show2 > 0) && t3 != "" ? " - " : "") + t3;
    text2 = showi ? ti : text1;
    text1 = showi && text1 == "" ? text2 : text1;
    text1 = fb.IsPaused ? "- PAUSE -" : text1;
    text2 = fb.IsPaused ? "- PAUSE -" : text2;    
    t_width2 = gr.CalcTextWidth(text2, font);
    t_width1 = gr.CalcTextWidth(text1, font);
    d = gr.CalcTextWidth("0000", font);
    
    if (mode) {
        step = sw == 1 ? step1 : step2;
        t_width = sw == 1 ? t_width1 : t_width2;
        if (sw == 1) {step2 = ww};
        if (sw == 2) {step1 = ww};

        var factor1 = Math.round(t_width * 0.01); // Min factor "s"
        var factor2 = Math.round(ww / 3);         // Max factor "s"
        var factor3 = ww - t_width - factor1 * 2; // Widh factor (ww VS text)
        var s = factor3 >= factor2 ? Math.min(factor1, factor2) : factor2;
        if (t_width < ww - s * 2) {
            if (step >= ww) {dir = "left"; step = ww };
            if (step + t_width >= ww - s) {dir = "left"};
            if (step <= 0 + s) {dir = "right"};
        } else {
            if (step + t_width <= ww - s) {dir = "right"};
            if (step >= 0 + s) {dir = "left"};
           }
        } else { 
        if (!reverse) {
            dir = "left";
        if (sw == 1) {
            if (step1 + t_width1 > ww / 2) {step2 = ww};
            if (step1 + t_width1 <= 0) {step1 = ww; sw = 2};
        } else {
            if (step2 + t_width2 > ww / 2) {step1 = ww};
            if (step2 + t_width2 <= 0) {step2 = ww; sw = 1};
           }
        } else {
            dir = "right";
        if (sw == 1) {
            if (step1 < ww / 2) {step2 = 0 - t_width2};          // {step2 = 0 - t_width2 - d};
            if (step1 >= ww) {step1 = 0 - t_width1; sw = 2};     // {step1 = 0 - t_width1 - d; sw = 2};
        } else {
            if (step2 < ww / 2) {step1 = 0 - t_width1};          // {step1 = 0 - t_width1 - d};
            if (step2 >= ww) {step2 = 0 - t_width2; sw = 1};     // {step2 = 0 - t_width2 - d; sw = 1};
        }
    }
}

// ========BACKGROUND & GRADIENT======== //
    if (!window.IsTransparent) gr.FillSolidRect(0, 0, ww, wh, backcolor_rgb);     // "backcolor_rgb" VS RGB(0,0,0)
    if ( window.IsTransparent) gr.FillGradRect(0, 0, ww, wh, 90, RGBA(0,0,0,150), RGBA(255,255,255,100), 1.0); // focus centre = 0.5 // High CPU usage
var b_w = 1;   // Border Width  
    gr.DrawRect(b_w/2, b_w/2, ww - b_w, wh - b_w, b_w, font_rgb);
// ========TEXT======== //
    gr.SetTextRenderingHint(4);
    gr.SetSmoothingMode(0)
    gr.DrawString(text1, font, font_rgb, step1, txt_row, t_width1 + d, wh);
    gr.DrawString(text2, font, font_rgb, step2, txt_row, t_width2 + d, wh);
}

// ========Mouse======== //
function on_mouse_mbtn_up(x,y) {
    window.SetProperty("|5| Scrolling Mode", !mode);
    mode = window.GetProperty("|5| Scrolling Mode");
}

function on_mouse_wheel(step) {
    step1 += step * 50, step2 += step * 50;
    reverse = step == 1 ? true : false;
    window.SetProperty("|8| Reverse Left to Right", reverse);
}

var ShiftDown;
function on_mouse_rbtn_down(x, y, vkey){
    ShiftDown = vkey==6 ? true : false;
}

// ========Menu======== //
function on_mouse_lbtn_up (x, y){
var _menu = window.CreatePopupMenu();
    _menu.AppendMenuItem(MF_STRING, 1, "Title");
    _menu.CheckMenuItem(1, show1);
    _menu.AppendMenuItem(MF_STRING, 2, "Artist");
    _menu.CheckMenuItem(2, show2);
    _menu.AppendMenuItem(MF_STRING, 3, "Album");
    _menu.CheckMenuItem(3, show3);
    _menu.AppendMenuItem(MF_STRING, 4, "Info");
    _menu.CheckMenuItem(4, showi);
    _menu.AppendMenuItem(MF_STRING, 5, "Mode:  " + (!mode ? (reverse ? "Right" : "Left") : "L <-> R")); // "Scrolling Mode:  "

    ret = _menu.TrackPopupMenu(x, y);
    if (ret == 0) return;

    switch (ret) {
    case 1:
        window.SetProperty("|1| Title", !show1);
        show1 = window.GetProperty("|1| Title");
        break;
    case 2:
        window.SetProperty("|2| Artist", !show2);
        show2 = window.GetProperty("|2| Artist");
        break;
    case 3:
        window.SetProperty("|3| Album", !show3);
        show3 = window.GetProperty("|3| Album");
        break;
    case 4:
        window.SetProperty("|4| Info", !showi);
        showi = window.GetProperty("|4| Info");
        break;
    case 5:
        window.SetProperty("|5| Scrolling Mode", !mode);
        mode = window.GetProperty("|5| Scrolling Mode");
        break;
}
    _menu.Dispose();
    return true;
}

function on_mouse_rbtn_up(x, y) {
var _menu = window.CreatePopupMenu();
    _menu.AppendMenuItem(MF_STRING, 1, "Default");
    _menu.AppendMenuItem(MF_STRING, 2, "Blue");
    _menu.AppendMenuItem(MF_STRING, 3, "Green");
    _menu.AppendMenuItem(MF_STRING, 4, "Red");
    _menu.AppendMenuItem(MF_STRING, 5, "White");
    _menu.AppendMenuItem(MF_STRING, 6, "Yellow");
    _menu.AppendMenuItem(MF_STRING, 7, "Grey");
    _menu.CheckMenuRadioItem(1, 7, window.GetProperty("|6| Color", 1));
    _menu.AppendMenuItem(MF_SEPARATOR, 0, 0)
    _menu.AppendMenuItem(MF_STRING, 8, "Properties...");
    if (ShiftDown){
     _menu.AppendMenuItem(MF_STRING, 9, "Configure...");
}
    idx = _menu.TrackPopupMenu(x, y);
    switch (idx) {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
        window.SetProperty("|6| Color", idx);
        window.Repaint();
        break;
    case 8:
        window.ShowProperties();
        break;
    case 9:
        window.ShowConfigure();
        break;
}
    _menu.Dispose();
    return true
}

// ========Timer======== //
var seek_timer;

function on_playback_new_track(info) {
   seek_timer && window.ClearInterval(seek_timer);
   seek_timer = window.SetInterval(function() {
    if (dir == "right") {step1 = step1 + 1};
    if (dir == "right") {step2 = step2 + 1};
    if (dir == "left") {step1 = step1 - 1};
    if (dir == "left") {step2 = step2 - 1};
    window.Repaint();
    }, speed );
}

function on_playback_stop() {
    window.ClearInterval(seek_timer);
    window.Repaint();
}

function on_playback_time(){
    if(!seek_timer) on_playback_new_track();
}